import { GCellRenderer } from "../../../../../com/mui/controls/GCellRenderer";
import { ObjCreate } from "../../common/ObjCreate";
import { GTextFormat } from "../../../../common/GTextFormat";
import { FontUtil } from "../../../../common/font/FontUtil";
import { GlobalStyle } from "../../../../common/global/GlobalStyle";
import { BaseItemNormal } from "../../common/item/BaseItemNormal";
import { ResFileConst } from "../../../resource/ResFileConst";
import { LoaderHelp } from "../../../../common/display/LoaderHelp";
import { ObjectPool } from "../../../../../com/gengine/utils/pools/ObjectPool";
import { ItemData } from "../../../resource/info/ItemData";
import { PloughCopyInfo } from "./PloughCopyInfo";
import { LoaderManager } from "../../../../../com/gengine/resource/LoaderManager";
import { LoaderPriority } from "../../../../../com/gengine/resource/LoaderPriority";
import { Global } from "../../../../../com/gengine/global/Global";
import { FilterConst } from "../../../../component/gconst/FilterConst";
import { ImageInfo } from "../../../../../com/gengine/resource/info/ImageInfo";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { GlobalClass } from "../../../../../com/mui/core/GlobalClass";
import { ImagesConst } from "../../../resource/ImagesConst";
import { GTextFiled } from "../../../../../com/mui/controls/GTextFiled";
import { GLoadedButton } from "../../../../../com/mui/controls/GLoadedButton";
//class PloughInfoItem
    
    export  class PloughInfoItem extends GCellRenderer
    {
        constructor()
        {
            super();
            this._randomList = [];
            return;
        }

        protected /* override */ configUI(): void
        {
            super.configUI();
            this._bg = ObjCreate.createBitmap(null, 0, 0, this);
            this._illusItemBmp = ObjCreate.createBitmap(null, 60, 45, this);
            this._cardBmp = ObjCreate.createBitmap(null, 55, 40, this);
            this._fixTitle = ObjCreate.createBitmap(null, 78, 227, this);
            this._randomTitle = ObjCreate.createBitmap(null, 78, 302, this);
            this._copyState = ObjCreate.createBitmap(null, 60, 45, this);
            var loc1=new GTextFormat(FontUtil.stXingkaiName, 20, 15788607, null, null, null, null, null, flash.text.TextFormatAlign.CENTER);
            this._nameTxt = ObjCreate.createGTextField("", 0, 3, 218, 35, this, loc1);
            this._copyDescStr = ObjCreate.createGTextField("", 0, 185, 224, 40, this, GlobalStyle.getTextFormatCenter(GlobalStyle.textFormatGreen));
            this._copyDescStr.wordWrap = true;
            this._copyDescStr.multiline = true;
            this._fixItem = new BaseItemNormal();
            this._fixItem.setSize(33, 33);
            this._fixItem.move(95, 255);
            addChild(this._fixItem);
            this._enterBtn = ObjCreate.createGLoadedButton(ResFileConst.PloughCopyEnterBtn, 70, 389, 89, 29, this);
            this._enterBtn.configEventListener(flash.events.MouseEvent.CLICK, this.onEnterBtnClickHandler);
            LoaderHelp.addResCallBack(ResFileConst.Illustrate, this.onResLoadedIllus);
            return;
        }

        /* internal  */createItems(arg1: Array<any>): void
        {
            var loc3=null;
            var loc1=10;
            var loc2=336;
            var loc4=0;
            while (loc4 < arg1.length) 
            {
                if (arg1[loc4] != 0) 
                {
                    (loc3 = ObjectPool.getObject(BaseItemNormal)).setSize(33, 33);
                    loc3.move(loc1, loc2);
                    loc3.itemData = new ItemData(parseInt(arg1[loc4]));
                    addChild(loc3);
                    this._randomList.push(loc3);
                    loc1 = loc1 + 40;
                }
                ++loc4;
            }
            return;
        }

        /* internal  */disposeItems(): void
        {
            var loc1=null;
            while (this._randomList.length > 0) 
            {
                loc1 = this._randomList[0] as BaseItemNormal;
                this._randomList.splice(0, 1);
                ObjectPool.disposeObject(loc1);
            }
            return;
        }

        public /* override */ set data(arg1: Object)
        {
            var loc1=null;
            var loc2=null;
            super.data = arg1;
            if (arg1 && arg1 is PloughCopyInfo) 
            {
                this._info = arg1 as PloughCopyInfo;
                if (this._info.tPloughCopy) 
                {
                    loc1 = this._info.tPloughCopy;
                    loc2 = new ItemData(loc1.illustrate);
                    if (loc2 && loc2.itemInfo) 
                    {
                        this._nameTxt.htmlText = loc2.itemInfo.item.name;
                        LoaderManager.instance.load(loc2.itemInfo.item.effectEx + ".jpg", this.onLoadCompleteHandler, LoaderPriority.LevelB);
                    }
                    this._copyDescStr.htmlText = loc1.descStr;
                }
                if (this._info.fixRewardList.length > 0 && this._info.fixRewardList[0] && this._info.fixRewardList[0] > 0) 
                {
                    this._fixItem.itemData = new ItemData(parseInt(this._info.fixRewardList[0]));
                }
                this.disposeItems();
                this.createItems(this._info.randomRewardList);
                if (this._info.copyState > 0 || Global.isDebugModle) 
                {
                    this._enterBtn.enabled = true;
                    this._enterBtn.filters = [];
                }
                else 
                {
                    this._enterBtn.enabled = false;
                    this._enterBtn.filters = [FilterConst.colorFilter2];
                }
                LoaderHelp.addResCallBack(ResFileConst.PloughCopy, this.onResLoadedCopy);
            }
            return;
        }

        /* internal  */onLoadCompleteHandler(arg1: ImageInfo): void
        {
            this._illusItemBmp.bitmapData = arg1.bitmapData;
            return;
        }

        /* internal  */onEnterBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._info && this._info.tPloughCopy) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.PloughCopyEnter, this._info.tPloughCopy.code));
            }
            return;
        }

        /* internal  */onResLoadedIllus(): void
        {
            this._cardBmp.bitmapData = GlobalClass.getBitmapData(ImagesConst.illustrateCardBg);
            return;
        }

        /* internal  */onResLoadedCopy(): void
        {
            if (this._bg.bitmapData == null) 
            {
                this._bg.bitmapData = GlobalClass.getBitmapData(ImagesConst.ploughItemBg);
            }
            if (this._fixTitle.bitmapData == null) 
            {
                this._fixTitle.bitmapData = GlobalClass.getBitmapData(ImagesConst.fixReward);
            }
            if (this._randomTitle.bitmapData == null) 
            {
                this._randomTitle.bitmapData = GlobalClass.getBitmapData(ImagesConst.randomReward);
            }
            if (this._info) 
            {
                this._copyState.bitmapData = GlobalClass.getBitmapData("ploughState_" + this._info.copyState);
            }
            return;
        }

        /* internal */ /* var */_info: PloughCopyInfo;

        /* internal */ /* var */_bg: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_cardBmp: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_fixTitle: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_randomTitle: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_illusItemBmp: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_copyState: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_nameTxt: GTextFiled;

        /* internal */ /* var */_copyDescStr: GTextFiled;

        /* internal */ /* var */_enterBtn: GLoadedButton;

        /* internal */ /* var */_fixItem: BaseItemNormal;

        /* internal */ /* var */_randomList: Array<any>;
    }
